var app = getApp()
var serverUrl=app.globalData.url;
var userinfo=app.globalData.userinfo;
console.info(userinfo)
var getDataURL=serverUrl+"/api/hr/kehuinfo/";
var getDataURLgj=serverUrl+"/api/hr/gengjing/";
var selectKhid="";
var  thisuid="0011"
var  thisuName="小飞";
var uploadImgPath=[];
var query;
var xiangqingId="";
var gengjinguid="";
Page({
  isdisableds:false,
  pagedescription:"添加客户跟进",
  gengjinguid:"",
  data: {
    isUsers:false,
    inputValue: '',
    items:[],
    sosuoDisplay:"none",
    gsname:"",
    datas:{
     id:"", 
beizhu:"",
gengjinggongsi:"",
gengjinggongsiName:"",
gengjingkehu:"",
gengjingkehuName:"",
gengjingtime:"",
gengjinguName:"",
gengjinguid:"",
gengjingxiangqing:"",
jihua:"",
status:"",
type:"1",
updateBy:"",
updateTime:"",
zongjie:""
    }
  },
  onReady(){
        userinfo=getApp().globalData.userinfo;
         thisuid=userinfo.userid;
         thisuName=userinfo.name;
         if(this.data.gengjinguid!= "" &&thisuid==this.data.gengjinguid){
           this.setData({
             isUsers:true,
             isdisableds:false
           })
         }
  },
  onLoad(query) { 
 // query=query;
    var that=this;
    if(query.xiangqingId!=undefined&& query.fromPage=="followHistory"  ){
      xiangqingId=query.xiangqingId;
           dd.httpRequest({ 
            url:getDataURLgj+'edit/'+query.xiangqingId,
            method:'get',
            datatype:'json',
            data:{},
            success:function(res){
              console.info(res.data.datas)
              if(res.data&&res.data.code==0&&res.data.datas.gengjinggongsiName.length>0){
                 console.info(res.data.datas.gengjinguid,">>>>******************11111111111111");
                  that.setData({
                    datas:res.data.datas,
                    gengjinguid:res.data.datas.gengjinguid,
                    inputValue:res.data.datas.gengjingkehuName,
                    gsname:res.data.datas.gengjinggongsiName,
                    pagedescription:"客户跟进详情",
                    isdisableds:true
                  })
              }
              console.info(res)
              // if(res.data.code==0){
              //    dd.showToast({
              //         content: '提交成功', // 文字内容
              //    });
              //  }else{
              //     dd.showToast({
              //         content: '提交失败', // 文字内容
              //    });
              //  }
            } 
    });
      }  
   },
  shangchuanwenjian(){


  },
  showHistorygj(e){
    my.navigateTo({
      url: `followHistory/followHistory`,
    });
  },
  selectkh(event){//点击选择搜到的客户
     console.info(event.target.dataset)    
      this.setData({
      inputValue: event.target.dataset.alphaBeta,
      sosuoDisplay:"block",
      gsname:event.target.dataset.gsname
       });
      selectKhid=event.target.dataset.alphaId;
  },
  pageTap(e){//点击页面事件
       console.info(e.target.id)
        if(e.target.id!="sousoukehu"||event.target.dataset.alphaId!=undefined||e.target.id!='inputKh'){
             this.setData({
               items:[],
               sosuoDisplay:"none"
             });
        }
  },
  bindKeyInput(e) { //输入客户时触发
     var dataThis=  e.detail.value; 
    this.setData({
      inputValue: e.detail.value,
    });
    var that=this;
    console.info(dataThis)
     dd.httpRequest({
          url: getDataURL+"list",
          method: 'POST',
          data: {
              remark :'getNameId' ,
              khname:dataThis
          },
          dataType: 'json',
          success: function(res) {
             
               console.info(res.data.rows)
            that.setData({
              items:res.data.rows,
               sosuoDisplay:"block"
            })
     }});
  },
  
  onSubmit(e) {
    var that = this;
    var dat=e.detail.value;
    var gengjingkehuName =dat.gengjingkehuName;
   if (!this.isnotNull(gengjingkehuName,"跟进的客户不能为空哦")){
     return ;
   }
    var gengjinggongsiName =e.detail.value.gengjinggongsiName;
     if (!this.isnotNull(gengjinggongsiName,"跟进的企业/公司不能为空哦")){
     return ;
   }
    var gengjingxiangqing =e.detail.value.gengjingxiangqing;
     if (!this.isnotNull(gengjingxiangqing,"跟进的详情不能为空哦")){
     return ;
   }
    var data={
      gengjingkehuName:gengjingkehuName,
      gengjinggongsiName:gengjinggongsiName,
      gengjingxiangqing:gengjingxiangqing,
      zongjie:dat.zongjie,
      jihua:dat.jihua,
      beizhu:dat.beizhu,
      gengjingkehu:selectKhid,
      gengjinguid:thisuid,
      gengjinguName :thisuName  
         }
    var reurl=getDataURLgj+'add';
    dd.httpRequest({ 
            url:reurl,
            method:'post',
            datatype:'json',
            data:data,
            success:function(res){
              console.info(res)
              if(res.data.code==0){
                 dd.showToast({
                      content: '提交成功', // 文字内容
                 });
                   data={ toAllUser:false,
                  useridList:"144069533138858074",
                  textContent:"1222222222222",
                  linkTitle:userinfo.name+"提交了一个客户跟进信息",
                  linkText:gengjinggongsiName+"的"+gengjingkehuName+"的跟进信息",
                  linkMessageUrl:"https://ding-doc.dingtalk.com/doc#/serverapi2/pgoxpy",
                  linkPicUrl:"dddddd"
                  }
           console.info(serverUrl+app.globalData.sendWorkUrl)
                  dd.httpRequest({ 
                    url:serverUrl+app.globalData.sendWorkUrl,
                    method:'post',
                    datatype:'json',
                    data:data,
                    success:function(res){
                      console.info(res)
                      if(res.data.code==0){
                        dd.showToast({
                              content: '提交成功', // 文字内容
                        });

                      }else{
                          dd.showToast({
                              content: '提交失败', // 文字内容
                        });
                      }
                    } 
            });
               }else{
                  dd.showToast({
                      content: '提交失败', // 文字内容
                 });
               }
            } 
    });
  },
  
  sendTextMsg(isAll,uids,textMgs){
           data={ toAllUser:isAll,
                  useridList:"144069533138858074",
                  textContent:textMgs
           }
           console.info(serverUrl+app.globalData.sendWorkUrl)
           dd.httpRequest({ 
            url:serverUrl+app.globalData.sendWorkUrl,
            method:'post',
            datatype:'json',
            data:data,
            success:function(res){
              console.info(res)
              if(res.data.code==0){
                 dd.showToast({
                      content: '提交成功', // 文字内容
                 });

               }else{
                  dd.showToast({
                      content: '提交失败', // 文字内容
                 });
               }
            } 
    });
  
  },
  isnotNull(val,smgs){
    console.info(val)
    console.info(smgs)
     if(val==null||val==undefined||val.trim()==""){
        dd.showToast({
          content: smgs, // 文字内容
        });
        return false;
     }
    return true;
  },

 chooseImage() {
dd.chooseImage({ //请求图片

count: 1,  //上传图片张数限制 

success: (res) => {

console.log("返回的文件信息为:");

console.log(res);

var filePaths = res.filePaths;

dd.getAuthCode({

success: function(res) {

console.log('filePath的值为:');

console.log(filePaths);

var authCode = res.authCode;

dd.uploadFile({

url: app.data.globalData.url + 'public/index.php/api/activity/upload_img',

fileType: 'image',

fileName: 'img',

filePath: filePaths[0],

formData: {

authCode: authCode

},

success: (res) => {

var data = JSON.parse(res.data);

console.log("转换后的data为:");

console.log(data);

var img_url = data.data.img_url;

that.data.actImg = img_url;

that.data.isUp = 1; //同时将是否已经上传改为是

console.log('图片信息为:');

console.log(that.data.actImg);

that.setData({

coverImg: that.data.actImg,

isUp: 1

})

},

fail: function(res) {

dd.showToast({

content: '上传信息失败',

type: 'fail',

});

},

});

},

fail: function(res) {

dd.showToast({

content: '获取用户权限信息失败',

type: 'fail',

});

}

})

},

});

},



 chooseImage2() {
    my.chooseImage({
      sourceType: ['camera', 'album'],
      count: 4,
      success: res => {
        console.info(res)
        uploadImgPath=[]
        for (let i in res.filePaths) {
             console.info(res.filePaths[i])
            uploadImgPath.push(res.filePaths[i]); //属性
            //arr.push(obj[i]); //值
        }
        console.info(typeof(res.filePaths))
          console.info(typeof(uploadImgPath))
        //  my.alert({
        //  content: JSON.stringify(res),
        //  });
      },
      fail: () => {
        my.showToast({
          content: '加载图片失败', // 文字内容
        });
      },
    });
  },
  previewImage() {
    console.info(uploadImgPath.join(","))
    my.previewImage({
      current: 4,
      urls:uploadImgPath,
    });
  },
  saveImage() {
    my.saveImage({
      url: 'https://img.alicdn.com/tps/TB1sXGYIFXXXXc5XpXXXXXXXXXX.jpg',
      showActionSheet: true,
      success: () => {
        my.alert({
          title: '保存成功',
        });
      },
    });
  },
  uploadFile() {
    my.chooseImage({
      chooseImage: 1,
      success: res => {
        const path = res.apFilePaths[0];
        console.log(path);
        my.uploadFile({
          url: 'http://httpbin.org/post',
          fileType: 'image',
          fileName: 'file',
          filePath: path,
          success: res => {
            my.alert({ title: '上传成功' });
          },
          fail: function(res) {
            my.alert({ title: '上传失败' });
          },
        });
      },
    });
  },
});
